package com.lilinling.yinzhi.user.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lilinling.common.exception.BizCodeEnum;
import com.lilinling.common.utils.JwtInfo;
import com.lilinling.common.utils.JwtUtils;
import com.lilinling.yinzhi.user.entity.UserEntity;
import com.lilinling.yinzhi.user.exception.MakerExsitException;
import com.lilinling.yinzhi.user.exception.MakerNameExsitException;
import com.lilinling.yinzhi.user.service.UserService;
import com.lilinling.yinzhi.user.vo.MakerInfoVo;
import com.lilinling.yinzhi.user.vo.MakerOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lilinling.yinzhi.user.entity.MakersEntity;
import com.lilinling.yinzhi.user.service.MakersService;
import com.lilinling.common.utils.PageUtils;
import com.lilinling.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 制作人
 *
 * @author lichenglin
 * @email lilinling15@gmail.com
 * @date 2021-03-06 21:36:27
 */
@Api("制作人接口")
//@ApiImplicitParams({@ApiImplicitParam(name = "makerId", value = "制作人ID", required = true, paramType="path", dataType = "Long")})
@RestController
@RequestMapping("user/makers")
public class MakersController {
    @Autowired
    private MakersService makersService;

    @Autowired
    private UserService userService;

    /**
     * 制作人申请：保存制作作人信息
     */
    @ApiOperation("制作人申请：保存制作作人信息")
    @RequestMapping("/auth/savemakerinfo")
    //@RequiresPermissions("user:makers:save")
    public R saveMakerInfo(@RequestBody MakerInfoVo makerInfoVo, HttpServletRequest request){
//		makersService.save(makers);
        JwtInfo jwtInfo = JwtUtils.getUserIdByJwtToken(request);
        Long userId = jwtInfo.getId();
        try{
            makersService.saveMakerInfo(userId,makerInfoVo);
        }catch (MakerExsitException e){
            return R.error(BizCodeEnum.MAKER_EXSIT_EXCEPTION.getCode(),BizCodeEnum.MAKER_EXSIT_EXCEPTION.getMsg());
        }catch (MakerNameExsitException e) {
            return R.error(BizCodeEnum.MAKERNAME_EXSIT_EXCEPTION.getCode(),BizCodeEnum.MAKERNAME_EXSIT_EXCEPTION.getMsg());
        }
        return R.ok("申请成功");
    }

    /**
     * 根据制作人id获取制作人信息
     */
    @ApiOperation("根据制作人id查询制作人信息")
    @GetMapping("/addmaker/{makerId}")
    public R getMakerByMakerId (@PathVariable("makerId") Long makerId) {
        MakerOrderVo makerOrderVo = makersService.getMakerOrderVoByMakerId(makerId);
        return R.ok().put("maker",makerOrderVo);
    }

    /**
     * 根据制作人id获取制作人信息（订单系统远程调用）
     */
    @ApiOperation("根据制作人id查询制作人信息（订单系统远程调用）")
    @GetMapping("/makerorder/{makerId}")
    public MakerOrderVo getMakerOrderVoByMakerId (@PathVariable("makerId") Long makerId) {
        MakerOrderVo makerOrderVo = makersService.getMakerOrderVoByMakerId(makerId);
        return makerOrderVo;
    }
    /**
     * 查询最新制作人
     */
    @ApiOperation("查询最新制作人")
    @GetMapping("/hotmaker/list")
    public R getHotMakerList () {
        QueryWrapper<MakersEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time").last("limit 4");
        List<MakersEntity> makersEntities = makersService.list(wrapper);
        return R.ok().put("hotmaker",makersEntities);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("user:makers:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = makersService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("user:makers:info")
    public R info(@PathVariable("id") Long id){
		MakersEntity makers = makersService.getById(id);

        return R.ok().put("makers", makers);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("user:makerinfo:save")
    public R save(@RequestBody MakersEntity maker){
        makersService.save(maker);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("user:makers:update")
    public R update(@RequestBody MakersEntity makers){
		makersService.updateById(makers);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("user:makers:delete")
    public R delete(@RequestBody Long[] ids){
		makersService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
